<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spark 简单测试</title>
    <style>
        body { 
            margin: 0; 
            padding: 20px;
            font-family: Arial, sans-serif;
            background: #1a1a1a;
            color: white;
        }
        #info {
            position: absolute;
            top: 10px;
            left: 10px;
            z-index: 100;
            background: rgba(0,0,0,0.8);
            padding: 10px;
            border-radius: 5px;
        }
        #canvas-container {
            width: 100%;
            height: 80vh;
            border: 1px solid #333;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <h1>Spark 库简单测试</h1>
    <div id="info">
        <div>状态: <span id="status">初始化中...</span></div>
        <div>错误: <span id="error">无</span></div>
    </div>
    <div id="canvas-container"></div>

    <script type="importmap">
        {
            "imports": {
                "three": "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.178.0/three.module.js",
                "@sparkjsdev/spark": "https://sparkjs.dev/releases/spark/0.1.9/spark.module.js"
            }
        }
    </script>

    <script type="module">
        import * as THREE from "three";
        import { SplatMesh } from "@sparkjsdev/spark";

        function updateStatus(message) {
            document.getElementById('status').textContent = message;
            console.log('状态:', message);
        }

        function updateError(message) {
            document.getElementById('error').textContent = message;
            console.error('错误:', message);
        }

        try {
            updateStatus('创建Three.js场景...');
            
            const scene = new THREE.Scene();
            const camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
            const renderer = new THREE.WebGLRenderer();
            
            const container = document.getElementById('canvas-container');
            const containerRect = container.getBoundingClientRect();
            renderer.setSize(containerRect.width, containerRect.height);
            container.appendChild(renderer.domElement);

            // 设置相机位置
            camera.position.set(0, 0, 5);

            updateStatus('测试SplatMesh创建...');

            // 测试创建空的SplatMesh
            const testSplat = new SplatMesh();
            scene.add(testSplat);
            
            updateStatus('SplatMesh创建成功！');

            // 尝试加载我们的SOG文件
            updateStatus('尝试加载SOG文件...');
            
            const sogSplat = new SplatMesh({
                url: './environments/libai_3dgs.sog',
                onLoad: (mesh) => {
                    updateStatus('SOG文件加载成功！');
                    console.log('加载的SplatMesh:', mesh);
                    
                    // 调整位置
                    mesh.position.set(0, 0, 0);
                    mesh.scale.set(1, 1, 1);
                }
            });
            
            scene.add(sogSplat);

            // 添加基本光照
            const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
            scene.add(ambientLight);

            const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
            directionalLight.position.set(1, 1, 1);
            scene.add(directionalLight);

            // 渲染循环
            function animate() {
                requestAnimationFrame(animate);
                
                // 旋转splat
                if (sogSplat) {
                    sogSplat.rotation.y += 0.005;
                }
                
                renderer.render(scene, camera);
            }

            animate();
            updateStatus('渲染循环启动成功！');

        } catch (error) {
            updateError(error.message);
            updateStatus('初始化失败');
        }

        // 处理窗口大小变化
        window.addEventListener('resize', () => {
            const container = document.getElementById('canvas-container');
            const containerRect = container.getBoundingClientRect();
            camera.aspect = containerRect.width / containerRect.height;
            camera.updateProjectionMatrix();
            renderer.setSize(containerRect.width, containerRect.height);
        });
    </script>
</body>
</html>